import 'package:xsdk_flutter/xsdk/utils/queue/x_bean_queue_message.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_log.dart';


/*
设备(socket)信息通知
单例,支持多页面监听
 */

//client socket设备连接状态
typedef XFunctionClientSocketDeviceConnectListener<T> = void Function(
    bool isConnect,
    String? ip,
    int? port,
    );
//server socket
typedef XFunctionServerSocketListener<T> = void Function(
    int currentClientSocketsCount,//当前子设备连接数
    );


//接收socket设备消息
typedef XFunctionReceiveSocketDeviceMessageListener<T> = void Function(
    String? ip,//对方ip
    int? port,//对方端口
    String? message
    );

//写入到socket设备状态
typedef XFunctionWriteMessageToSocketDeviceListener<T> = void Function(
    bool isSuccess,
    String? ip,
    XBaseQueueMessage? msg,
    );


class XSocketDeviceNotifyCallback{

  XSocketDeviceNotifyCallback._();

  static final XSocketDeviceNotifyCallback _instance= XSocketDeviceNotifyCallback._();
  static XSocketDeviceNotifyCallback get instance => _instance;

  //支持多页面socket监听//////////////////////////////////////////////////////////////////////

  //添加Socket设备连接状态监听
  void addServerSocketDeviceConnectListener(XFunctionServerSocketListener? listener){
    if(listener!=null){
      _serverSocketDeviceConnectListeners.add(listener);
    }
  }
  void addClientSocketDeviceConnectListener(XFunctionClientSocketDeviceConnectListener? listener){
    if(listener!=null){
      _clientSocketDeviceConnectListeners.add(listener);
    }
  }
  //删除Socket设备连接状态监听
  void removeClientSocketDeviceConnectListener(XFunctionClientSocketDeviceConnectListener? listener){
    if(listener!=null){
      _clientSocketDeviceConnectListeners.remove(listener);
    }else{
      _clientSocketDeviceConnectListeners.clear();
    }

  }
  void removeServerSocketDeviceConnectListener(XFunctionServerSocketListener? listener){
    if(listener!=null){
      _serverSocketDeviceConnectListeners.remove(listener);
    }else{
      _serverSocketDeviceConnectListeners.clear();
    }
  }

  //添加 写入内容到Socket设备状态监听
  void addServerSocketDeviceWriteMessageListener(XFunctionWriteMessageToSocketDeviceListener? listener){
    if(listener!=null){
      _serverSocketDeviceWriteMessageListeners.add(listener);
    }
  }
  void addClientSocketDeviceWriteMessageListener(XFunctionWriteMessageToSocketDeviceListener? listener){
    if(listener!=null){
      _clientSocketDeviceWriteMessageListeners.add(listener);
    }
  }
  //删除 写入内容到Socket设备状态监听
  void removeServerSocketDeviceWriteMessageListener(XFunctionWriteMessageToSocketDeviceListener? listener){
    if(listener!=null){
      _serverSocketDeviceWriteMessageListeners.remove(listener);
    }else{
      _serverSocketDeviceWriteMessageListeners.clear();
    }
  }
  void removeClientSocketDeviceWriteMessageListener(XFunctionWriteMessageToSocketDeviceListener? listener){
    if(listener!=null){
      _clientSocketDeviceWriteMessageListeners.remove(listener);
    }else{
      _clientSocketDeviceWriteMessageListeners.clear();
    }
  }

  //添加 接收Socket设备消息监听
  void addServerSocketDeviceReceiveMessageListener(XFunctionReceiveSocketDeviceMessageListener? listener){
    if(listener!=null){
      _serverSocketDeviceReceiveMessageListeners.add(listener);
    }
  }
  void addClientSocketDeviceReceiveMessageListener(XFunctionReceiveSocketDeviceMessageListener? listener){
    if(listener!=null){
      _clientSocketDeviceReceiveMessageListeners.add(listener);
    }

  }
  //删除 接收Socket设备消息监听
  void removeServerSocketDeviceReceiveMessageListener(XFunctionReceiveSocketDeviceMessageListener? listener){
    if(listener!=null){
      _serverSocketDeviceReceiveMessageListeners.remove(listener);
    }else{
      _serverSocketDeviceReceiveMessageListeners.clear();
    }
  }
  void removeClientSocketDeviceReceiveMessageListener(XFunctionReceiveSocketDeviceMessageListener? listener){
    if(listener!=null){
      _clientSocketDeviceReceiveMessageListeners.remove(listener);
    }else{
      _clientSocketDeviceReceiveMessageListeners.clear();
    }

  }

  //私有/////////////////////////////////////////////////////////////////////////

  //XSDK内部调用, socket设备 //////////////////////////////////////////////////////////////////////

  final List<XFunctionClientSocketDeviceConnectListener> _clientSocketDeviceConnectListeners=[];
  final List<XFunctionServerSocketListener> _serverSocketDeviceConnectListeners=[];
  //接收socket设备消息监听
  final List<XFunctionReceiveSocketDeviceMessageListener> _clientSocketDeviceReceiveMessageListeners=[];
  final List<XFunctionReceiveSocketDeviceMessageListener> _serverSocketDeviceReceiveMessageListeners=[];

  //写入内容到BLE蓝牙设备状态监听
  final List<XFunctionWriteMessageToSocketDeviceListener> _clientSocketDeviceWriteMessageListeners=[];
  final List<XFunctionWriteMessageToSocketDeviceListener> _serverSocketDeviceWriteMessageListeners=[];


  //clientSocket连接状态
  void clientSocketDeviceConnectStatusCallback(
      bool isConnect,
      String? ip,
      int? port,
      ){
    XLogUtils.printLog('---clientSocketDeviceConnectStatusCallback,isConnect=$isConnect, ip=$ip, port=$port');
    try{
      for(XFunctionClientSocketDeviceConnectListener listener in _clientSocketDeviceConnectListeners){
        listener(isConnect,ip,port);
      }
    }catch(e){
      //
    }
  }
  void serverSocketDeviceConnectStatusCallback(
      int currentClientSocketsCount,
      ){
    XLogUtils.printLog('---serverSocketDeviceConnectStatusCallback,currentClientSocketsCount=$currentClientSocketsCount');
    try{
      for(XFunctionServerSocketListener listener in _serverSocketDeviceConnectListeners){
        listener(currentClientSocketsCount);
      }
    }catch(e){
      //
    }
  }

  //接收socket消息
  void receiveSocketDeviceMsgCallback(bool isServer,String? ip, int? port, String? message){
    XLogUtils.printLog('---receiveSocketDeviceMsgCallback, isServer=$isServer,ip=$ip, port=$port, message=$message');
    try{
      if(isServer){
        //服务端接收消息
        for(XFunctionReceiveSocketDeviceMessageListener listener in _serverSocketDeviceReceiveMessageListeners){
          listener(ip,port,message);
        }
      }else{
        //客户端
        for(XFunctionReceiveSocketDeviceMessageListener listener in _clientSocketDeviceReceiveMessageListeners){
          listener(ip,port,message);
        }
      }

    }catch(e){
      //
    }
  }

  //写入消息到socket设备结果回调
  void writeMsgToSocketDeviceCallback({
    required bool isServer,//是否服务端写入的消息,否则就是客户端写入的消息
    required bool isSuccess,
    required String? ip,
    XBaseQueueMessage? msg,
  }){
    XLogUtils.printLog('---writeMsgToSocketDeviceCallback 写入socket设备结果: isSuccess=$isSuccess, ip=$ip');

    try{
      if(isServer){
        for(XFunctionWriteMessageToSocketDeviceListener listener in _serverSocketDeviceWriteMessageListeners){
          listener(isSuccess,ip,msg);
        }
      }else{
        for(XFunctionWriteMessageToSocketDeviceListener listener in _clientSocketDeviceWriteMessageListeners){
          listener(isSuccess,ip,msg);
        }
      }

    }catch(e){
      //
    }
  }


}